<template>
  <div>
    <!-- 导航条 -->
    <nav-bar>
      <div slot="left">返回</div>
      <div slot="title">登陆页</div>
    </nav-bar>

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="formData.username"
        name="name"
        label="用户名"
        placeholder="用户名"
        required
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="formData.password"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        required
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
    data(){
        return{
            formData:{},
        }
    },
    methods:{
        API_login(data){
            return this.$utils.$http.request({
                url:"login",
                method:"POST",
                data
            })
        },
        async onSubmit(values){
            let res = await this.API_login(values)
            if(res.data.code !== 1000){
                this.$toast.fail(res.data.msg);
            }else{
                this.$toast.success(res.data.msg);
                localStorage.setItem("jwtToken",res.data.token)
                this.$router.replace("/")
            }
        }
    }
};
</script>

<style></style>